package com.example.my.agiftthat.ui.classify.fragment;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.HorizontalScrollView;

import com.example.my.agiftthat.R;
import com.example.my.agiftthat.config.Url;
import com.example.my.agiftthat.helper.OkHttpClientHelper;
import com.example.my.agiftthat.helper.decoration.SpacesItemDecoration;
import com.example.my.agiftthat.ui.classify.adapter.ClassRecyclerAdapter;
import com.example.my.agiftthat.ui.classify.adapter.ColumnRecyclerAdapter;
import com.example.my.agiftthat.ui.classify.bean.ColumnBean;
import com.example.my.agiftthat.ui.classify.bean.ThreeBean;
import com.example.my.agiftthat.ui.home.adapter.SendRecyclerAdapter;
import com.example.my.agiftthat.ui.home.bean.SendBean;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by My on 2016/12/22.
 */

public class StrategyFragment extends Fragment {
    private Context mContext = null;
    private RecyclerView recyclerView_column, recyclerView_class, recyclerView_style, recyclerView_object;
    private List<ColumnBean.DataBean.ColumnsBean> mList = new ArrayList<>();
    private ColumnRecyclerAdapter adapter = null;
    private List<ThreeBean.DataBean.ChannelGroupsBean.ChannelsBean> tList = new ArrayList<>();
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    mList = (List<ColumnBean.DataBean.ColumnsBean>) msg.obj;
                    recyclerView_column.setHasFixedSize(true);
                    GridLayoutManager manager = new GridLayoutManager(mContext, 3);
                    manager.setOrientation(GridLayoutManager.HORIZONTAL);
                    recyclerView_column.setLayoutManager(manager);
                    adapter = new ColumnRecyclerAdapter(mContext, mList);
                    recyclerView_column.setAdapter(adapter);
                    break;
            }
        }
    };
    private Handler handler1 = new Handler() {
        @Override
        public void handleMessage(Message msg1) {
            super.handleMessage(msg1);
            switch (msg1.what) {
                case 1:
                    tList = (List<ThreeBean.DataBean.ChannelGroupsBean.ChannelsBean>) msg1.obj;
                    recyclerView_class.setHasFixedSize(true);
                    GridLayoutManager manager = new GridLayoutManager(mContext, 2);
                    recyclerView_class.setLayoutManager(manager);
                    ClassRecyclerAdapter adapter = new ClassRecyclerAdapter(mContext, tList);
                    recyclerView_class.setAdapter(adapter);
                    break;
            }
        }
    };
    private Handler handler2 = new Handler() {
        @Override
        public void handleMessage(Message msg2) {
            super.handleMessage(msg2);
            switch (msg2.what) {
                case 2:
                    tList = (List<ThreeBean.DataBean.ChannelGroupsBean.ChannelsBean>) msg2.obj;
                    recyclerView_style.setHasFixedSize(true);
                    GridLayoutManager manager = new GridLayoutManager(mContext, 2);
                    recyclerView_style.setLayoutManager(manager);
                    ClassRecyclerAdapter adapter = new ClassRecyclerAdapter(mContext, tList);
                    recyclerView_style.setAdapter(adapter);
                    break;
            }
        }
    };
    private Handler handler3 = new Handler() {
        @Override
        public void handleMessage(Message msg3) {
            super.handleMessage(msg3);
            switch (msg3.what) {
                case 3:
                    tList = (List<ThreeBean.DataBean.ChannelGroupsBean.ChannelsBean>) msg3.obj;
                    recyclerView_object.setHasFixedSize(true);
                    GridLayoutManager manager = new GridLayoutManager(mContext, 2);
                    recyclerView_object.setLayoutManager(manager);
                    ClassRecyclerAdapter adapter = new ClassRecyclerAdapter(mContext, tList);
                    recyclerView_object.setAdapter(adapter);
                    break;
            }
        }
    };

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getActivity();
        reloadNetworkData();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_strategy, null);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        recyclerView_column = (RecyclerView) view.findViewById(R.id.recyclerView_column);
        recyclerView_class = (RecyclerView) view.findViewById(R.id.recyclerView_class);
        recyclerView_style = (RecyclerView) view.findViewById(R.id.recyclerView_style);
        recyclerView_object = (RecyclerView) view.findViewById(R.id.recyclerView_object);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

    }

    private void reloadNetworkData() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    String jsonStr = OkHttpClientHelper.getStringFromURL(mContext, Url.CLASSIFY_LM, 3);
                    String jsonString = OkHttpClientHelper.getStringFromURL(mContext, Url.CLASSIFY_PFD, 3);
                    Gson gson = new Gson();
                    ColumnBean columnBean = gson.fromJson(jsonStr, ColumnBean.class);
                    ThreeBean threeBean = gson.fromJson(jsonString, ThreeBean.class);
                    mList = columnBean.getData().getColumns();
                    tList = threeBean.getData().getChannel_groups().get(0).getChannels();
                    List<ThreeBean.DataBean.ChannelGroupsBean.ChannelsBean> fList=threeBean.getData().getChannel_groups().get(1).getChannels();
                    List<ThreeBean.DataBean.ChannelGroupsBean.ChannelsBean> dList=threeBean.getData().getChannel_groups().get(2).getChannels();
                    Message msg = new Message();
                    msg.what = 0;
                    msg.obj = mList;
                    handler.sendMessage(msg);

                    Message msg1 = new Message();
                    msg1.what = 1;
                    msg1.obj = tList;
                    handler1.sendMessage(msg1);

                    Message msg2 = new Message();
                    msg2.what = 2;
                    msg2.obj = fList;
                    handler2.sendMessage(msg2);

                    Message msg3 = new Message();
                    msg3.what = 3;
                    msg3.obj = dList;
                    handler3.sendMessage(msg3);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}
